package initAll;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
import seleniumUnit.linkOperate;

public class eventsListener implements WebDriverEventListener{
	private static final Logger log = LogManager.getLogger(eventsListener.class.getName());
	
	//使用WebDriver过程中报错时执行
	@Override
	public void onException(Throwable throwable, WebDriver driver) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String dateString = format.format(new Date());
		String fileName="test-output/"+dateString+".jpg";
		linkOperate.ScreenshotRobot(fileName);
        log.error("cause by:"+throwable.getMessage());
        log.error("image filePath:"+fileName);      
	}

	//findElement（findElements）方法后执行
	@Override
	public void afterFindBy(By by, WebElement element, WebDriver driver) {
		log.debug("afterFindBy; element-Info="+element.toString());
	}
	
	//使用findElement（findElements）方法前
	@Override
	public void beforeFindBy(By by, WebElement element, WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("beforeFindBy; element-Info="+element.toString());
	}
	
	//点击后执行
	@Override
	public void afterClickOn(WebElement element, WebDriver driver) {
		log.debug("afterClickOn; element-Info="+element.toString());
	}

	//点击前执行
	@Override
	public void beforeClickOn(WebElement element, WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("beforeClickOn; element-Info="+element.toString());
	}
	
	//使用forward方法操作浏览器前进后执行
	@Override
	public void afterNavigateForward(WebDriver driver) {
		log.debug("afterNavigateForward; URL=" + driver.getCurrentUrl() + "  title=" + driver.getTitle());
	}
	
	//使用forward方法操作浏览器前进前执行
	@Override
	public void beforeNavigateForward(WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("beforeNavigateForward; URL=" + driver.getCurrentUrl() + "  title=" + driver.getTitle());
	}
	
	//使用switchTo切换句柄后执行
	@Override
	public void afterSwitchToWindow(String windowName, WebDriver driver) {
		log.debug("afterSwitchToWindow;  URL=" + driver.getCurrentUrl() + "  title=" + driver.getTitle());
	}

	//使用switchTo切换句柄前执行
	@Override
	public void beforeSwitchToWindow(String windowName, WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("beforeSwitchToWindow;  URL=" + driver.getCurrentUrl() + "  title=" + driver.getTitle());
	}

	//使用get在浏览器上输入url访问前执行
	@Override
	public void beforeNavigateTo(String url, WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("beforeNavigateTo;URL="+url);
	}
	
	//使用get在浏览器上输入url访问后执行
	@Override
	public void afterNavigateTo(String url, WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("afterNavigateTo;URL="+url);	
	}
	
	//使用back方法操作浏览器返回前执行
	@Override
	public void beforeNavigateBack(WebDriver driver) {
		// TODO Auto-generated method stub	
		log.debug("beforeNavigateBack;URL="+driver.getCurrentUrl());
	}
	
	//使用back方法操作浏览器返回后执行
	@Override
	public void afterNavigateBack(WebDriver driver) {
		// TODO Auto-generated method stub	
		log.debug("afterNavigateBack;URL="+driver.getCurrentUrl());
	}
	
	//操作Alert窗口确认前执行
	@Override
	public void beforeAlertAccept(WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("beforeAlertAccept;URL="+driver.getCurrentUrl());
	}

	//操作Alert窗口确认后执行
	@Override
	public void afterAlertAccept(WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("afterAlertAccept;URL="+driver.getCurrentUrl());
	}

	//操作Alert窗口取消后执行
	@Override
	public void afterAlertDismiss(WebDriver driver) {
		// TODO Auto-generated method stub	
		log.debug("afterAlertDismiss;URL="+driver.getCurrentUrl());
	}

	//操作Alert窗口取消前执行
	@Override
	public void beforeAlertDismiss(WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("beforeAlertDismiss;URL="+driver.getCurrentUrl());
	}

	//使用refresh方法操作浏览器刷新前执行
	@Override
	public void beforeNavigateRefresh(WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("beforeNavigateRefresh;URL="+driver.getCurrentUrl());
	}

	//使用refresh方法操作浏览器刷新后执行
	@Override
	public void afterNavigateRefresh(WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("afterNavigateRefresh;URL="+driver.getCurrentUrl());
	}

	//输入值前执行
	@Override
	public void beforeChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {
		// TODO Auto-generated method stub
		log.debug("beforeAlertDismiss;URL="+driver.getCurrentUrl()+"  element="+element.toString()+"  key="+keysToSend.toString());
	}

	//输入值后执行
	@Override
	public void afterChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {
		// TODO Auto-generated method stub
		log.debug("afterChangeValueOf;URL="+driver.getCurrentUrl()+"  element="+element.toString()+"  key="+keysToSend.toString());
	}

	//使用JavaScript前执行
	@Override
	public void beforeScript(String script, WebDriver driver) {
		// TODO Auto-generated method stub
	}

	//使用JavaScript后执行
	@Override
	public void afterScript(String script, WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public <X> void beforeGetScreenshotAs(OutputType<X> target) {
		// TODO Auto-generated method stub
	}

	@Override
	public <X> void afterGetScreenshotAs(OutputType<X> target, X screenshot) {
		// TODO Auto-generated method stub
	}

	@Override
	public void beforeGetText(WebElement element, WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public void afterGetText(WebElement element, WebDriver driver, String text) {
		// TODO Auto-generated method stub
	}

}
